import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';

import '../services/screen_adapter.dart';
import './product_content/product_content_first.dart';
import './product_content/product_content_second.dart';
import './product_content/product_content_third.dart';
import '../widget/mq_button.dart';
import '../model/product_content.dart';
import '../config/config.dart';
import '../widget/loading_widget.dart';
import '../services/event_bus.dart';
import '../provider/cart.dart';
import '../services/cart_service.dart';

class ProductContentPage extends StatefulWidget {

  final Map arguments;

  ProductContentPage({Key key, this.arguments}) : super(key: key);

  @override
  _ProductContentPageState createState() => _ProductContentPageState();
}

class _ProductContentPageState extends State<ProductContentPage> {

  List _productContentList = [];

  @override
  void initState() {
    super.initState();
    _getContentData();
  }

  _getContentData() async{
    var api = "${Config.domain}api/pcontent?id=${widget.arguments['id']}";
    var result = await Dio().get(api);
    var productContent = ProductContentModel.fromJson(result.data);
    setState(() {
      _productContentList.add(productContent.result);
    });
  }

  @override
  Widget build(BuildContext context) {
    ScreenAdapter.init();
    return DefaultTabController(
      length: 3, 
      child: Scaffold(
        appBar: AppBar(
          title: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Container(
                width: ScreenAdapter.width(400),
                child: TabBar(
                  indicatorColor: Colors.red, //指示器颜色
                  indicatorSize: TabBarIndicatorSize.label, //指示器长度与文字宽度一样
                  tabs: <Widget>[
                    Tab(
                      child: Text('商品')
                    ),
                    Tab(
                      child: Text('详情')
                    ),
                    Tab(
                      child: Text('评价')
                    )
                  ]
                )
              )
            ],
          ),
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.more_horiz),
                onPressed: () {
                  showMenu(
                    context: context, 
                    position: RelativeRect.fromLTRB(ScreenAdapter.width(600), 76, 10, 0), 
                    items: [
                      PopupMenuItem(
                        child: Row(
                          children: <Widget>[
                            Icon(Icons.home),
                            Text('首页')
                          ]
                        )
                      ),
                      PopupMenuItem(
                        child: Row(
                          children: <Widget>[
                            Icon(Icons.search),
                            Text('搜索')
                          ]
                        )
                      )
                    ]
                  );
                }
              )
          ],
        ),
        body: _productContentList.length > 0 ? Stack(
          children: <Widget>[
            TabBarView(
              children: <Widget>[
                ProductContentFirst(_productContentList),
                ProductContentSecond(_productContentList),
                ProductContentThird()
              ],
              physics: NeverScrollableScrollPhysics() //禁止TabBarView滑动
            ),
            Positioned(
              width: ScreenAdapter.width(750),
              height: ScreenAdapter.height(88),
              bottom: 0,
              child: Container(
                decoration: BoxDecoration(
                  border: Border(
                    top: BorderSide(
                      color: Colors.black26,
                      width: 1
                    )
                  ),
                  color: Colors.white
                ),
                child: Row(
                  children: <Widget>[
                    InkWell(
                      onTap: () => Navigator.pushNamed(context, '/cart'),
                      child: Container(
                        padding: EdgeInsets.only(top: ScreenAdapter.height(8)),
                        width: ScreenAdapter.width(120),
                        height: ScreenAdapter.height(80),
                        child: Column(
                          children: <Widget>[
                            Icon(Icons.shopping_cart, size: ScreenAdapter.size(36)),
                            Text('购物车', style: TextStyle(
                              fontSize: ScreenAdapter.size(22)
                            ))
                          ],
                        )
                      )
                    ),
                    
                    Expanded(
                      flex: 1,
                      child: MqButton(
                        color: Color.fromRGBO(253, 1, 0, 0.9),
                        text: '加入购物车',
                        callback: () async{
                          if(_productContentList[0].attr.length > 0) {
                            // 广播
                            eventBus.fire(ProductContentEvent('加入购物车'));
                          } else {
                            await CartService.addCart(_productContentList[0]);
                            /// 调用provider更新数据
                            context.read<Cart>().updateList();
                            Fluttertoast.showToast(
                              msg: "加入购物车成功",
                              toastLength: Toast.LENGTH_SHORT,
                              gravity: ToastGravity.CENTER,
                              timeInSecForIosWeb: 1,
                              backgroundColor: Colors.grey[600],
                              textColor: Colors.white,
                              fontSize: 16.0
                            );
                          }
                          
                        }
                      )
                    ),

                    Expanded(
                      flex: 1,
                      child: MqButton(
                        color: Color.fromRGBO(255, 165, 0, 0.9),
                        text: '立即购买',
                        callback: () {
                          
                          if(_productContentList[0].attr.length > 0) {
                            // 广播
                            eventBus.fire(ProductContentEvent('立即购买'));
                          } else {
                            print('立即购买操作');
                          }
                          
                        }
                      )
                    ),

                  ]
                )
              )
            )
          ],
        ) : LoadingWidget()
      )
    );
  }
}